/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Lifeline;

/**
 * Command to set the Represents attribute of a Lifeline.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Lifeline
 * <li>Represents - if null, it means any existing type will be unset.
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>None
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class SetRepresentsCommand extends Command {
	private Lifeline lifeline;
	private ConnectableElement represents, oldRepresents;

	public SetRepresentsCommand() {
	}

	public SetRepresentsCommand(final Lifeline lifeline,
			final ConnectableElement represents) {
		setLifeline(lifeline);
		setRepresents(represents);
	}

	public void setLifeline(final Lifeline lifeline) {
		this.lifeline = lifeline;
	}

	public void setRepresents(final ConnectableElement represents) {
		this.represents = represents;
	}

	@Override
	public void execute() {
		oldRepresents = lifeline.getRepresents();
		lifeline.setRepresents(represents);
	}

	@Override
	public void undo() {
		lifeline.setRepresents(oldRepresents);
		oldRepresents = null;
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void dispose() {
		lifeline = null;
		represents = null;
		oldRepresents = null;
	}
}
